<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="guestbook.Greeting" %>
<%@ page import="guestbook.PMF" %>

<html>
<head>
<title>GuestBook</title>

    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />       
  </head>

  <body background="http://i1201.photobucket.com/albums/bb352/nitsoy1/wallpaper-169935.jpg">


<script LANGUAGE="JavaScript1.1">

function derecha(e) {
if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)){
alert('Botón derecho bloqueado.')
return false;
}

else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2)){
alert('Botón derecho bloqueado.')
}
}
document.onmousedown=derecha
</script>


<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hola, <%= user.getNickname() %>! (ahora puedes
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">desconectarte</a>.)</p>
<%
    } else {
%>
<p>Hola!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Inicia sesion</a>
para incluir tu mensajes con tu nick.</p>
<%
    }
%>

<%
    PersistenceManager pm = PMF.get().getPersistenceManager();

	String query = "select from " + Greeting.class.getName() + " order by date desc range 0,5";
	
    List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute();
    if (greetings.isEmpty()) {
%>
	<p>The guestbook has no messages.</p>
<%
    } else {
        for (Greeting g : greetings) {
            if (g.getAuthor() == null) {
%>
<p>Un usuario anonimo escribió:</p>
<%
            } else {
%>
<p><b><%= g.getAuthor().getNickname() %></b> escribió:</p>
<%
            }
%>
<blockquote><%= g.getContent() %></blockquote>
<%
        }
    }
    pm.close();
%>

    <form action="/sign" method="post">
      <div align=center><textarea name="content" rows="3" cols="60" ></textarea></div>
      <div align=center><input type="submit"  value="Post Greeting" onmouseover="this.style.color='blue'" onmouseout="this.style.color='black'"></div>
    </form>
  </body>
</html>